const {ipcMain}=require('electron')
const robot=require('robotjs')
const vkey=require('vkey')

function handleMouse(data){
  // console.log(data)
  let {clientX,clientY,screen,video}=data
  console.log(clientX,clientY)
  let x=clientX*screen.width/video.width
  let y=clientY*screen.height/video.height
  robot.moveMouseSmooth(x,y)
  robot.mouseClick()
}
function handleKey(data){
  console.log(data)
  const modifiers=[]
  if(data.meta) modifiers.push('meta')
  if(data.shift)modifiers.push('shift')
  if(data.alt)modifiers.push('alt')
  if(data.ctrl)modifiers.push('ctrl')
  inputStr = String.fromCharCode(data.keyCode);
  let key=vkey[data.keyCode].toLowerCase()
  console.log(key)
  robot.keyTap(inputStr,modifiers)
}

module.exports=function(){
  ipcMain.on('robot',(e,type,data)=>{
 
    if(type==='mouse'){
      handleMouse(data)
    }
    else if(type==='key'){
      handleKey(data)
    }
  })
}